package com.cloud.bc.service.impl;

import com.cloud.bc.model.entity.BcSlide;
import com.cloud.bc.mapper.BcSlideMapper;
import com.cloud.bc.service.BcSlideService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.List;

/**
 * 轮播图
 *
 * @author douhaichao code generator
 * @date 2021-01-07 21:54:30
 */
@Service
@AllArgsConstructor
public class BcSlideServiceImpl extends ServiceImpl<BcSlideMapper, BcSlide> implements BcSlideService {

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    @Override
    public BcSlide selectOne(BcSlide req) {
        QueryWrapper queryWrapper = new QueryWrapper<BcSlide>();
        queryWrapper.setEntity(req);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据条件查询所有
     */
    @Override
    public List<BcSlide> selectAll(BcSlide req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        queryWrapper.orderByDesc("sort");
        queryWrapper.orderByDesc("id");
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据对象查询所有分页数据(精确查询)     *      * @return
     */
    @Override
    public IPage<BcSlide> selectPage(Page page, BcSlide req) {
        IPage<BcSlide> list = baseMapper.selectPage(
                page,
                new QueryWrapper<BcSlide>().setEntity(req));
        return list;
    }

    /**
     * 根据对象查询所有分页数据(模糊查询)     *      * @return
     */
    @Override
    public IPage<BcSlide> selectPageByLike(Page page, BcSlide req) {
        QueryWrapper queryWrapper = new QueryWrapper();

        if (null != req.getId()) {
            queryWrapper.eq("id", req.getId());
        }

        if (!StrUtil.isEmpty(req.getTitle())) {
            queryWrapper.like("title", req.getTitle());
        }

        if (!StrUtil.isEmpty(req.getImg())) {
            queryWrapper.like("img", req.getImg());
        }

        if (!StrUtil.isEmpty(req.getUrl())) {
            queryWrapper.like("url", req.getUrl());
        }

        if (null != req.getCreateTime()) {
            queryWrapper.eq("create_time", req.getCreateTime());
        }

        if (null != req.getIsDel()) {
            queryWrapper.eq("is_del", req.getIsDel());
        }
        IPage<BcSlide> list = baseMapper.selectPage(page, queryWrapper);
        return list;
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    @Override
    public int count(BcSlide req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectCount(queryWrapper);
    }
}
